package cms.simmytech.web.cms;

import cms.simmytech.model.TbCategory;
import cms.simmytech.model.TbContent;
import cms.simmytech.service.CategoryService;
import cms.simmytech.service.ContentService;
import cms.simmytech.web.base.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/content")
public class ContentController extends BaseController {
    @Autowired
    ContentService contentService;
    @Autowired
    CategoryService categoryService;
    //内容分页显示界面对应的控制层方法（关键字查询-根据内容标题、内容类型）
    @RequestMapping(value = "/list")
    public String userList(@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Model model,
                           @RequestParam(value = "name" ,required = false,defaultValue = "") String name,
                           @RequestParam(value = "contentType" ,required = false) Byte contentType,
                           @RequestParam(value = "cateId" ,defaultValue = "-99") Integer cateId) {
        //对参数进行判空处理
        contentType=  contentType==null?-99:contentType;
        //调用业务逻辑层的分页查询语句
        PageInfo<TbContent> pageInfo = contentService.getPageList(pageNum, pageSize,name,contentType,cateId);
        List<TbCategory>    catelist =categoryService.getAllList(  ((byte) 1));
        //将查询结果调价到Model中
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name", name);
        model.addAttribute("contentType", contentType);
        model.addAttribute("catelist", catelist);
        model.addAttribute("cateId", cateId);
        //返回展示界面
        return super.toPage("content/list", model);
    }
    //内容添加（更新）控制层响应方法
    @RequestMapping(value = "/add")
    public String add(Integer id, Integer pId, String dictType, Model model) {
        //查询所有的类别信息，在界面中进行展示
        List<TbCategory>    catelist =categoryService.getAllList(  ((byte) 1));
        TbContent bean = null;
        //如果前端传递来一个Id，（ID为空）证明是更新操作，否则就是添加操作
        if (id == null) {
            bean = new TbContent();
        } else {
            //根据Id查询内容信息
            bean = contentService.getBean(id);

        }
        //将相关信息（分类信息、内容信息）添加到前后端共享的Model中
        model.addAttribute("bean", bean);
        model.addAttribute("catelist", catelist);
        model.addAttribute("webPath",super.webPath);
        //跳转到更新（添加）界面
        return super.toPage("content/edit", model);
    }
    //内容添加（更新）响应的控制层方法
    @PostMapping(value = "/save")
    public String saveUser(TbContent bean) {
        //调用业务逻辑层的保存方法
        contentService.save(bean);
        return "redirect:list";//重定向展示界面
    }
    //响应内容删除的控制层方法
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> delete(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用业务逻辑层的根据内容Id进行删除
        contentService.delete(id);
        map.put("res", true);
        return map;
    }

}
